class UsersController < ApplicationController
  before_filter :user_authorize,:except => [:getSendersComboBox]
  layout "index",:except => [:getUsers,:addUser,:delUser,:editUser,:getSendersComboBox,:getOnlineUsers,
  :chatRecordList,:getChatRecordList,:getAllUsersComboBox,:smsRecordList,:getSMSRecordList]
  
  def index
    @users = User.find(:all,:conditions=>["is_locked <> 1"])
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end
  
  # GET /users/1
  # GET /users/1.xml
  def show
    @user = User.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end
  
  # GET /users/new
  # GET /users/new.xml
  def new
    @user = User.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end
  
  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end
  
  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])
    
    respond_to do |format|
      if @user.save
        flash[:notice] = 'User was successfully created.'
        format.html { redirect_to(@user) }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /users/1
  # PUT /users/1.xml
  def update
    @user = User.find(params[:id])
    
    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    
    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end
  
  #List Uers Main Page
  def userList
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
  end
  
  def getUsers
    begin
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      strdelData = params[:delData]
      if strdelData!=nil
        delUser(strdelData)
      end
      pageUsers = User.find(:all,:conditions=>["is_locked <> 1 AND user_type <> 'super_admin'"])
      users = User.find(:all,:conditions=>["is_locked <> 1 AND id <> 1 AND user_type <> 'super_admin'"],:limit =>pageBegin+","+pageEnd)
      jsonstr="{totalProperty:"+pageUsers.length.to_s+",root:"+users.to_json(:include=>[:department])+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Add User
  def addUser
    begin
      
      user = User.new
      user.user_name=params[:user_name]
      user.password=params[:password]
      user.employee_no=params[:employee_no]
      user.login_id=params[:login_id]
      user.user_type=params[:user_type]
      user.department_id=params[:department_id]
      user.eterm_name = params[:eterm_name]
      user.eterm_pswd = params[:eterm_pswd]
      user.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Delete User
  def delUser(strdelData)
    ids=""
    begin
      
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      User.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  # Get User By ID
  def getUserByID
    begin
      
      user = User.find(params[:id])
      jsonstr="{root:["+user.to_json(:include=>[:department])+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Edit User
  def editUser
    begin
      
      user = User.find(params[:id])
      user.user_name=params[:user_name]
      user.password=params[:password]
      user.employee_no=params[:employee_no]
      user.login_id=params[:login_id]
      user.user_type=params[:user_type]
      user.eterm_name = params[:eterm_name]
      user.eterm_pswd = params[:eterm_pswd]
      if params[:department_id]!=""
        user.department_id=params[:department_id]
      end
      user.save
      #jsonstr="{root:"+user.to_json()+"}"
      #render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getSendersComboBox
    begin
      users = User.find(:all,:conditions=>["is_locked <> 1 AND user_type='送票员'"])
      jsonstr = "{root:"+users.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getOnlineUsers
    begin
      department_id=params[:department_id]
      if department_id!=nil
        users = User.find(:all,:conditions=>["is_locked <> 1 AND is_online = 1 AND id <> ? AND department_id = ?",session[:user_id],department_id.to_i])
      else
        users = User.find(:all,:conditions=>["is_locked <> 1 AND is_online = 1 AND id <> ?",session[:user_id]])
      end
      jsonstr="{totalProperty:"+users.length.to_s+",root:"+users.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def chatRecordList
    @user_id = params[:user_id]
  end
  
  def smsRecordList
    @user_id = params[:user_id]
  end
  
  
  def getChatRecordList
    begin
      
      chatContent = ChatContent.find(:all,:conditions=>["chat_to like ? OR chat_from = ? OR chat_to = ?","%|"+params[:user_id].to_s+"|%",params[:user_id],"ALL"])
      jsonstr="{totalProperty:"+chatContent.length.to_s+",root:"+chatContent.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getSMSRecordList
    begin
      
      sms = Sms.find(:all,:conditions=>["user_id = ?",params[:user_id]])
      jsonstr="{totalProperty:"+sms.length.to_s+",root:"+sms.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def getAllUsersComboBox
    begin
      
      users = User.find(:all,:conditions=>["is_locked <> 1"])
      jsonstr="{root:"+users.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #My CallersLog
  def callersLogList
    
  end
  
  def getMyCallersLog
    begin
      
      callersLogs = CallersLog.find(:all,:conditions=>["user_id = ?",session[:user_id]],:order=>"id Desc")
      jsonstr="{root:"+callersLogs.to_json(:include=>[:customer,:user])+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
end
